<?php include "classes.inc" ?>
<?php include "constants.inc" ?>
<?php

  if (isset($_GET['match'])) {
    $userInput = $_GET['match'];
  }

  if (isset($_GET['team_id'])) {
    $teamId = $_GET['team_id'];
  }

  $connection = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
	mysql_select_db(DB_CATALOG);
	$query = sprintf("select zp.zt_player_id, zp.index_nr from zt_team t,
    zt_player zp where t.team_id = zp.team_id and t.team_id = %u
    order by zp.index_nr", $teamId);
  $result = mysql_query($query, $connection);
  $playerItems = array();
	while ($row = mysql_fetch_assoc($result)) {
    if ( isset($userInput) && !empty($userInput) ) {
      if (strpos(strtoupper($row['index_nr']), strtoupper($userInput)) === 0) {
        $keys[] = $row['zt_player_id'];
        $values[] = $row['index_nr'];
        $playerItems = array_combine($keys, $values);
    	}
    } else {
      $keys[] = $row['zt_player_id'];
      $values[] = $row['index_nr'];
      $playerItems = array_combine($keys, $values);
    }
	}

  echo json_encode($playerItems);
?>